class Grab < ActiveRecord::Base
	belongs_to :brand,  :foreign_key => "brand_id"
	validates_presence_of :title, :detail, :price, :quantity,  :message => "can't be blank"
	validates_numericality_of :price, :quantity
	validates_uniqueness_of :title, :message => "You have already added this item, do you need update the quantity"
	validate :price_must_at_least_a_cent
	validate :expire_date_must_later_than_bought_date
	validate :quantity_must_more_than_one
	
	def from_time
		from_time=Time.now
	end
	def to_time
		to_time=@grab.expire_date
	end
	
	protected
		def price_must_at_least_a_cent
			errors.add(:price, 'should be at least 0.01') if price.nil? ||
			price < 0.01
		end
		
		def expire_date_must_later_than_bought_date
			errors.add(:expire_date, "Are you sure you haven't bought the wrong stuff?") if bought_date >= expire_date
		end
		
		def quantity_must_more_than_one
			errors.add(:quantity, "You must really have one of this item") if quantity.nil? ||
			quantity < 1
		end
end
